import { _decorator, Component, Node } from 'cc';
import { MessageServer } from '../message/MessageServer';
import { MessageClient } from '../message/MessageClient';
const { ccclass, property } = _decorator;

@ccclass('Test1')
export class Test1 extends Component {
    server: MessageServer = null!;

    client: MessageClient = null!;

    protected onLoad(): void {
        this.init();
    }

    init() {
        this.server = new MessageServer(globalThis.window);

        this.client = new MessageClient();

        this.server.onMessage('message_1', this.serverMessage);
        this.client.onMessage('message_2', this.clientMessage);
    }

    onClientSend() {
        console.log('onClient click');
        this.client.sendMessage('message_1', { data: 'client' });
    }

    onServerSend() {
        console.log('onServe click');
        this.server.sendMessage('message_2', { data: 'server' });
    }

    serverMessage(data: any) {
        console.log('serverMessage', data);
    }

    clientMessage(data: any) {
        console.log('clientMessage', data);
    }
}
